/*
 * @Author: xiaosihan
 * @Date: 2023-02-06 15:22:57
 * @Last Modified by: xiehangyunn
 * @Last Modified time: 2025-07-24 21:10:12
 */
import { AmbientLight, DirectionalLight, PointLight, Vector3 } from 'three';
import ThreeBase from 'three-base';
import threeUtils from 'three-base/threeUtils';

import GLTF from './component3d/GLTF/GLTF';
import GLTF_GLB from './component3d/GLTF/GLTF.glb?url';
/**
 * @class HomeRender
 * @extends {ThreeBase}
 */
class HomeRender extends ThreeBase {
    constructor() {
        super();
    }

    // 环境光
    ambientLight = (() => {
        const ambientLight = new AmbientLight('#fff', 1.5);
        this.scene.add(ambientLight);
        return ambientLight;
    })();

    //点光源
    pointLight = (() => {
        const pointLight = new PointLight('#fff', 1.5);
        pointLight.position.set(50, 40, 50);
        pointLight.shadow.bias = -0.001;
        this.scene.add(pointLight);
        return pointLight;
    })();

    // 方向光
    directionalLight = (() => {
        const directionalLight = new DirectionalLight('#fff', 1.5);
        directionalLight.position.set(-10, 10, 5);
        directionalLight.target.position.set(0, 0, 0);
        directionalLight.castShadow = true;
        directionalLight.shadow.bias = -0.001;
        directionalLight.shadow.mapSize.set(1024, 1024);
        this.scene.add(directionalLight);
        return directionalLight;
    })();

    init() {
        super.init();

        this.autoRender = false;

        // 开发模式时还原记录的位置
        if (threeUtils.isDev && localStorage.lookAt) {
            this.resetLookAt({
                ...JSON.parse(localStorage.lookAt),
            });
        }

        // 开发模式时记录当前的位置
        if (threeUtils.isDev) {
            this.controls.addEventListener('change', () => {
                localStorage.lookAt = JSON.stringify(this.getLookAt());
            });
        }

        if (threeUtils.isProd || (threeUtils.isDev && !localStorage.lookAt)) {
            this.resetLookAt({
                center: new Vector3(0, 0, 0),
                position: new Vector3(-7.049, 27.469, 59.276),
            });
        }
    }


    //天空
    // sky = (() => {
    //   const sky = new Sky();
    //   this.scene.add(sky);
    //   return sky;
    // })();

    // 加载 gltf
    gltf = (() => {
        const gltf = new GLTF(GLTF_GLB);
        gltf.addEventListener("loaded", () => {
            console.log("gltf 加载完成");
            this.render();
            //@ts-ignore
            if (window.notifyPuppeteer) {
                //@ts-ignore
                window.notifyPuppeteer();
            }
        });
        gltf.scale.set(10, 10, 10);
        this.scene.add(gltf);
        return gltf;
    })();

    //meshbase
    // meshbase = (() => {
    //     const meshbase = new MeshBase(GLTF_GLB);
    //     this.scene.add(meshbase);
    //     return meshbase;
    // })();



}

const homeRenderer = new HomeRender();

export default homeRenderer;
